<?php
/**
 * Enter description here ...
 * @author Kevin
 *
 */
class Bet {
	
	//======================
	// Attributes
	//======================

	var $id = NULL;
	var $event = NULL;
	var $type = NULL;
	var $tip = NULL;
	var $option = NULL;
	var $quote = NULL;
	var $winning = NULL;
	var $exist = false;
	
	//======================
	// Constructors
	//======================
	
	/**
	 * Bet
	 * @param int $pId
	 */
	function Bet(int $pId){
		global $wpdb;
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM bets WHERE id = %d", $pId));
		
		$this->id = $pId;
		if($_row->id > 0){
			$this->event = $_row->fk_event;
			$this->type = $_row->fk_bettype;
			$this->tip = $_row->tip;
			$this->option = $_row->option;
			$this->quote = $_row->quote;
			$this->winning = $_row->winning;
			
			$this->exist = true;
		}
	}
	
	//======================
	// Methods
	//======================
	
	/**
	 * Enter description here ...
	 */
	function create(){
		global $wpdb;
		 
		$queryTable = 'bets';
		$queryData = array( 
			'id' => $this->id, 
			'fk_event' => $this->event,
			'fk_bettype' => $this->type,
			'tip' => $this->tip,
			'option' => $this->option,
			'quote' => $this->quote,
			'winning' => 0
		);
		$queryDataFormat = array('%d','%d','%d','%s','%s','%f','%d');
		
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
	}
	
	/**
	 * Enter description here ...
	 */
	function save(){
		global $wpdb;
		 
		$queryTable = 'bets';
		$queryData = array( 
			'fk_event' => $this->event,
			'fk_bettype' => $this->type,
			'tip' => $this->tip,
			'option' => $this->option,
			'quote' => $this->quote,
			'winning' => $this->winning
		);
		$queryDataFormat = array('%d','%d','%s','%s','%f','%d');		
		$queryCondition = array("id" => $this->id);		
		$queryConditionFormat = array('%d');
		
		$wpdb->update( $queryTable, $queryData, $queryCondition, $queryDataFormat, $queryConditionFormat);
	}
	
	//======================
	// Accessors
	//======================
	
	function getId(){
		return $this->id;
	}
	
	function getEvent(){
		return new Event($this->event);
	}
	
	function setEvent($pEvent){
		$this->event = $pEvent;
	}
	
	function getType(){
		return $this->type;
	}
	
	function setType($pType){
		$this->type = $pType;
	}
	
	function getTypeName(){
		global $wpdb;
		
		$brutName = 
		$wpdb->get_var(
			$wpdb->prepare(
				// requete
				"SELECT name FROM bettypes WHERE id = %d",
				// parametres
				$this->type
			)
		);
		
		$translatedName = getTranslation("bets", $brutName);		
		
		// remplacement des zone parametrable
		$_OPTION_ = $this->option;
		$_OPTIONINVERSE_ = - floatval($this->option);
		
		$initial = array("%OPTION%", "%OPTIONINVERSE%");
		$final   = array($_OPTION_, $_OPTIONINVERSE_);				
		$return = str_replace($initial, $final, $translatedName);
		
		return $return;
	}
	
	function getTip(){
		$brutTip = $this->tip;
		
		if($this->type == 33) return $brutTip;	// score exact
		
		$translatedTip = getTranslation("bets", $brutTip);
		
		// remplacement des zone parametrable
		$_OPTION_ = $this->option;
		$_TEAM1_ = "1";
		$_TEAM2_ = "2";
		
		$initial = array("%TEAM1%", "%TEAM2%", "%OPTION%");
		$final   = array($_TEAM1_, $_TEAM2_, $_OPTION_);				
		$return = str_replace($initial, $final, $translatedTip);
		
		return $return;
	}
	
	function setTip($pTip){
		$this->tip = $pTip;
	}	
		
	function getOption(){
		return $this->option;
	}
	
	function setOption($pOption){
		$this->option = $pOption;
	}
	
	function getQuote(){
		return number_format($this->quote,2);
	}
	
	function setQuote($pQuote){
		$this->quote = $pQuote;
	}
	
	function getExist(){
		return $this->exist;
	}
	
	function setExist($pValue){
		$this->exist = $pValue;
	}
	
	function isWinning(){
		return $this->winning;
	}
	
	function updateWinning(){

	}
	
	
}